package com.kamistoat.meimeistore.modulessystem.service.impl;

import com.kamistoat.meimeistore.apisystem.domain.SysPost;
import com.kamistoat.meimeistore.apisystem.entity.SysUserPostEntity;
import com.kamistoat.meimeistore.commoncore.constant.UserConstants;
import com.kamistoat.meimeistore.commoncore.utils.StringUtils;
import com.kamistoat.meimeistore.commoncore.utils.page.Query;
import com.kamistoat.meimeistore.modulessystem.dao.SysPostDao;
import com.kamistoat.meimeistore.modulessystem.service.SysPostService;
import com.kamistoat.meimeistore.modulessystem.service.SysUserPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kamistoat.meimeistore.commoncore.utils.page.PageUtils;
import com.kamistoat.meimeistore.apisystem.entity.SysPostEntity;


@Service("sysPostService")
public class SysPostServiceImpl extends ServiceImpl<SysPostDao, SysPostEntity> implements SysPostService {

    @Autowired
    SysUserPostService sysUserPostService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysPostEntity> page = this.page(
                new Query<SysPostEntity>().getPage(params),
                new QueryWrapper<SysPostEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysPostEntity> selectPostList(SysPost sysPost) {
        QueryWrapper<SysPostEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotNull(sysPost.getPostCode())) {
            queryWrapper.like("post_code", sysPost.getPostCode());
        }
        if (StringUtils.isNotNull(sysPost.getStatus())) {
            queryWrapper.eq("status", sysPost.getStatus());
        }
        if (StringUtils.isNotNull(sysPost.getPostName())) {
            queryWrapper.like("post_name", sysPost.getPostName());
        }
        return this.list(queryWrapper);
    }

    /**
     * 获取用户拥有的normal状态的post
     *
     * @param userId
     */
    @Override
    public List<SysPostEntity> selectPostListUnderNormal(Long userId) {
        List<Long> postIds = sysUserPostService
                .list(new QueryWrapper<SysUserPostEntity>().eq("user_id", userId))
                .stream().map(SysUserPostEntity::getPostId)
                .collect(Collectors.toList());
        if (postIds == null || postIds.size() == 0) {
            return new ArrayList<>();
        }
        return this
                .list(new QueryWrapper<SysPostEntity>().in("post_id", postIds))
                .stream().filter(sysPostEntity -> sysPostEntity.getStatus().equals(UserConstants.NORMAL))
                .collect(Collectors.toList());

    }

    @Override
    public List<SysPostEntity> selectPostListByUserId(Long userId) {
        List<SysUserPostEntity> user_post = sysUserPostService.list(
                new QueryWrapper<SysUserPostEntity>().eq("user_id", userId));
        List<Long> postIds = user_post.stream().map(SysUserPostEntity::getPostId).collect(Collectors.toList());
        return this.list(new QueryWrapper<SysPostEntity>().in("post_id", postIds));
    }

    @Override
    public SysPostEntity selectPostByPostId(Long postId) {
        return this.getOne(new QueryWrapper<SysPostEntity>().eq("post_id", postId));
    }

    /**
     * 检查岗位名称是否唯一。同时考虑新增和修改的情况
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        SysPostEntity one = this.getOne(new QueryWrapper<SysPostEntity>().eq("post_name", post.getPostName()));
        return StringUtils.isNotNull(one) && !one.getPostId().equals(post.getPostId());
    }

    /**
     * 检查岗位代码是否唯一，同时考虑新增和修改的情况
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        SysPostEntity one = this.getOne(new QueryWrapper<SysPostEntity>().eq("post_code", post.getPostCode()));
        return StringUtils.isNotNull(one) && !one.getPostId().equals(post.getPostId());
    }


}